/*
 * Copyright (c) Jack魏 2024 - 2024, All Rights Reserved.
 */

package demo;

import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Toolkit;

/**
 * 窗口实例
 *
 * @author Jack魏
 * @since 2024-06-16
 */
public class WindowsDemo {
    public static void main(String[] args) {
        // 1.创建窗体框架
        JFrame frame = new JFrame("居中示例");
        // 2.获取屏幕长和宽尺寸
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int screenWidth = screenSize.width;
        int screenHeight = screenSize.height;

        // 3.设置窗体大小
        int frameWidth = 500;
        int frameHeight = 500;
        frame.setSize(frameWidth, frameHeight);

        // 4.窗体居中对齐算法
        int x = (screenWidth - frameWidth) / 2;
        int y = (screenHeight - frameHeight) / 2;
        frame.setLocation(x, y);

        // 5.设置窗体关闭时的默认操作，没有这个点击X不会停止后台运行！！！
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 6.最佳布局大小
        frame.pack();

        // 7.别忘了显示Frame
        frame.setVisible(true);
    }
}
